package cn.com.jd.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import cn.com.jd.common.CommonConstant;
import cn.com.jd.dto.RegisterDto;
import cn.com.jd.dto.StatusDto;
import cn.com.jd.service.RegLogInService;

@Controller
@SessionAttributes("userId")
public class RegLogInController {

	private static Logger logger = Logger.getLogger(RegLogInController.class); 
	@Resource
	private RegLogInService regLogInService;
	
	@RequestMapping({"/"})
	public String entry(){
		return "register";
	}
	
	@RequestMapping({"/jumptologin"})
	public String jumpToRegister(){
		return "login";
	}
	
	@RequestMapping(value="/register",method= RequestMethod.POST)
	public ModelAndView register(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
		RegisterDto rd = new RegisterDto();
		rd.setUser_id((String)request.getParameter("userid"));
		rd.setPassword((String)request.getParameter("password"));
		rd.setInvited_cd((String)request.getParameter("invitedcd"));
		StatusDto statusdto = regLogInService.register(rd);
		Map<String,Object> map = new HashMap<String,Object>();
		if(CommonConstant.NORMAL == statusdto.getSts()){
			model.addAttribute("userId", (String)request.getParameter("userid"));
			map.put("userid", rd.getUser_id());
			return new ModelAndView("usermanage",map);
		} else {
			map.put("msg", statusdto.getMsg());
			return new ModelAndView("register",map);
		}
	}
	
	@RequestMapping(value="/login",method= RequestMethod.POST)
	public ModelAndView login(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
		RegisterDto rd = new RegisterDto();
		rd.setUser_id((String)request.getParameter("userid"));
		rd.setPassword((String)request.getParameter("password"));
		StatusDto statusdto = regLogInService.login(rd);
		Map<String,Object> map = new HashMap<String,Object>();
		if(CommonConstant.NORMAL == statusdto.getSts()){
			map.put("userid", rd.getUser_id());
			model.addAttribute("userId", (String)request.getParameter("userid"));
			return new ModelAndView("usermanage",map);
		} else {
			map.put("msg", statusdto.getMsg());
			return new ModelAndView("login",map);
		}
	}
	
	@RequestMapping("/checkIdentifyingCd")
    public String checkIdentifyingCd(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws IOException {
        Map<String,Object> map = new HashMap<String,Object>(); 
        String identifyingCd = (String)request.getParameter("identifyingCd");
        if (!(identifyingCd.equalsIgnoreCase(session.getAttribute("code").toString()))) {  //忽略验证码大小写
		    map.put("sts", 100);
        }else {
		    map.put("sts", 0);
        }
	    String json = JSONObject.fromObject(map).toString();
	    //将数据返回  
	    response.setCharacterEncoding("UTF-8");  
	    response.flushBuffer();  
        response.getWriter().write(json);  
        response.getWriter().flush();    
        response.getWriter().close();
        return null;
    }
}
